﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/BasicInformationCustom")]
    public class BasicInformationCustomController : ApiController
    {
        B_BasicInformationCustom b;
        public BasicInformationCustomController()
        {
            b = new B_BasicInformationCustom();
        }

        /// <summary>
        /// 获取模板自定义信息数据（测试专用）
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData()
        {

            B_BasicInformation a = new B_BasicInformation();
            var data = from i in b.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.BasicID equals p.BasicID
                       orderby i.BICID ascending
                       select new BasicInformationCustom
                       {
                           BICID = i.BICID,
                           BICName = i.BICName,
                           BICDescribe = i.BICDescribe,
                           BasicID = i.BasicID
                       };
            var list = data.ToList();
            return Ok(new Result<List<BasicInformationCustom>> { Data = list});
        }

        /// <summary>
        /// 获取模板信息自定义数据
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetAll(int page,int size)
        {

            B_BasicInformation a = new B_BasicInformation();
            var data = from i in b.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.BasicID equals p.BasicID
                       orderby i.BICID ascending
                       select new BasicInformationCustom
                       {
                           BICID = i.BICID,
                           BICName = i.BICName,
                           BICDescribe = i.BICDescribe,
                           BasicID = i.BasicID
                       };
            return Ok(new Result<List<BasicInformationCustom>> {
                Data = data.Skip((page-1)*size).Take(size).ToList(),
                Msg=data.Count().ToString()
            });
        }

        /// <summary>
        /// 根据用户信息ID查询与其相关联的用户自定义信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByBasicInform/{id}")]
        [HttpGet]
        public IHttpActionResult FindByBasicInform(int id)
        {
            var data = from i in b.GetAll().Where(x=>x.BasicID==id)
                       select new Param_BasicInformCus
                       {
                           BasicID = i.BasicID,
                           BICName = i.BICName,
                           BICDescribe = i.BICDescribe,
                           BICID = i.BICID
                       };
            return Ok(new Result<List<Param_BasicInformCus>>() {
                Data = data.ToList()
            });
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpPost]
        public IHttpActionResult Delete(int id)
        {
            if (b.FindById(id)==null)
            {
                b.Delete(id);
                return Ok(new Result<object> { Code = 200 });
            }
            else
            {
                return Ok(new Result<object> { Msg = "不存在" });
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="basic"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(BasicInformationCustom basic)
        {
            var data = new BasicInformationCustom
            {
                BICName = basic.BICName,
                BICDescribe = basic.BICDescribe,
                BasicID = basic.BasicID,
            };
            b.Add(data);
            return Ok(new Result<object> { });
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("FindByID/{id}")]
        public IHttpActionResult FindByID(int id)
        {
            if (b.GetAll().Count(x => x.BICID == id) > 0)
            {
                var data = b.FindById(id);
                return Ok(new Result<BasicInformationCustom>
                {
                    Data = new BasicInformationCustom
                    {
                       BICID = data.BICID,
                       BICName = data.BICName,
                       BICDescribe = data.BICDescribe,
                       BasicID = data.BasicID,
                    },
                });
            }
            else
            {
                return Ok(new Result<object> { Msg = "不存在", Code = 404 });
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="basic"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, BasicInformationCustom basic)
        {
            if (b.FindById(id)==null)
            {
                return Ok(new Result<object> { Msg = "不存在", Code = 404 });
            }
            b.Update(id, basic);
            return Ok(new Result<object> { });
        }
    }
}
